package com.lzkj.baotouhousingfund.widget.recyclerview;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.base.BaseFragment;
import com.lzkj.baotouhousingfund.base.BasePresenter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.lang.ref.WeakReference;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;


public abstract class XRecyclerLinearFragment<T extends BasePresenter, V> extends BaseFragment<T> implements OnRefreshLoadmoreListener {

    protected RecyclerView mRecyclerView;
    protected RefreshLayout  mRefreshLayout;

    protected Adapter<V> mAdapter;

    private View mNotDataView;
    private View mErrorView;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycler;
    }

    @CallSuper
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recyclerView);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        mRefreshLayout.setEnableRefresh(getRefreshEnabled());
        mRefreshLayout.setEnableLoadmore(getLoadMoreEnabled());
        mRefreshLayout.setPrimaryColorsId(R.color.colorTheme,R.color.colorWhite);
        mRefreshLayout.setOnRefreshLoadmoreListener(this);//设置读取与读取更多
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), VERTICAL));

        mAdapter = new Adapter<>(getItemLayoutId(), this);
        mRecyclerView.setAdapter(mAdapter);


        mNotDataView = getActivity().getLayoutInflater().inflate(R.layout.view_empty, (ViewGroup) mRecyclerView.getParent(), false);
//        mNotDataView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                refresh();
//            }
//        });
        mErrorView = getActivity().getLayoutInflater().inflate(R.layout.view_error, (ViewGroup) mRecyclerView.getParent(), false);
//        mErrorView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                refresh();
//            }
//        });
    }


    protected boolean getRefreshEnabled() {
        return true;
    }

    protected boolean getLoadMoreEnabled() {
        return true;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    protected void onItemClick(RecyclerView recycler, View view, int position, long id, V item) {
    }

    private boolean onItemLongClick(RecyclerView recycler, View view, int position, long itemId, V item) {
        return false;
    }

    protected void onSearchClick(String text) {
    }


    protected void onBindViewEvent(final BaseViewHolder helper, final V item) {
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick(mRecyclerView, view, helper.getLayoutPosition(), helper.getItemId(), item);
            }
        });
        helper.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return onItemLongClick(mRecyclerView, view, helper.getLayoutPosition(), helper.getItemId(), item);
            }
        });
    }

    protected abstract void convert(BaseViewHolder helper, V item);

    protected abstract int getItemLayoutId();

    public void notifyDataSetChanged() {
        if (mRecyclerView != null && mRecyclerView.getAdapter() != null) {
            mRecyclerView.getAdapter().notifyDataSetChanged();
        }
    }

    public void setErrorView(){
        mAdapter.setEmptyView(mErrorView);
    }

    public void setNotDataView(){
        mAdapter.setEmptyView(mNotDataView);
    }

    /**
     * 开始刷新
     */
    public void refresh() {
        mRefreshLayout.autoRefresh();
    }

    /**
     * 完成刷新
     */
    public void finishRefresh() {
        mRefreshLayout.finishRefresh();
    }

    /**
     * 读取结束
     */
    public void finishLoadmore() {
        mRefreshLayout.finishLoadmore();
    }

    /**
     * 全部数据加载完成
     */
    public void setLoadmoreFinished(boolean isFinished) {
        mRefreshLayout.setLoadmoreFinished(isFinished);
    }

    /**
     * 设置刷新是否可用
     *
     * @param enable
     */
    public void setEnableRefresh(boolean enable) {
        mRefreshLayout.setEnableRefresh(enable);
    }

    /**
     * 设置读取更多是否可用
     *
     * @param enable
     */
    public void setEnableLoadMore(boolean enable) {
        mRefreshLayout.setEnableLoadmore(enable);
    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {

    }

    @Override
    public void onLoadmore(RefreshLayout refreshLayout) {

    }

    protected static class Adapter<V> extends BaseQuickAdapter<V, ViewHolder> {
        private WeakReference<XRecyclerLinearFragment> mReference;

        Adapter(int layoutResId, XRecyclerLinearFragment fragment) {
            super(layoutResId);
            mReference = new WeakReference<>(fragment);
        }

        @Override
        protected void convert(ViewHolder helper, V item) {
            XRecyclerLinearFragment fragment = mReference.get();
            if (fragment != null) fragment.convert(helper, item);
            fragment.onBindViewEvent(helper, item);
        }

    }

    private static class ViewHolder extends BaseViewHolder {

        public ViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);
        }
    }
}
